package com.shanyue.redwine.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;

import java.util.HashMap;
import java.util.Stack;

/**
 * Created by fei on 2016/7/25.
 */
public class AppTaskManager {
    // Activity栈
    private static Stack<Activity> activityStack;
    // 单例模式
    private static AppTaskManager instance;
    private static HashMap<String, Activity> activityHashMap;

    public AppTaskManager() {
        activityHashMap = new HashMap<>();
    }

    /**
     * 单一实例
     */
    public static AppTaskManager getAppManager() {
        if (instance == null) {
            instance = new AppTaskManager();
        }
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
        String name = getActivityName(activity);
        activityHashMap.put(name, activity);
    }

    /**
     * 获取当前Activity（堆栈中最后一个压入的）
     */
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }

    public Activity findActivity(String name) {
        if (activityHashMap.containsKey(name)) {
            return activityHashMap.get(name);
        }
        return null;
    }

    /**
     * 获取栈的长度
     */
    public static int getTaskLength() {
        return activityStack.size();
    }

    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            String name = getActivityName(activity);
            activityHashMap.remove(name);
            activity.finish();
            activity = null;
        }
    }

    public void finishActivity(String name) {
        try {
            Activity activity = activityHashMap.get(name);
            activityStack.remove(activity);
            activityHashMap.remove(name);
            activity.finish();
            activity = null;
        } catch (Exception ex) {
            return;
        }
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }

    /**
     * 结束所有Activity
     */
    public static void finishAllActivity() {
        for (int i = 0; i < activityStack.size(); i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityHashMap.clear();
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public static void AppExit(Context context) {
        try {
            finishAllActivity();
//            ActivityManager activityMgr = (ActivityManager) context
//                    .getSystemService(Context.ACTIVITY_SERVICE);
//            assert activityMgr != null;
//            activityMgr.killBackgroundProcesses(context.getPackageName());
            //            System.exit(0);
        } catch (Exception e) {
        }
    }

    private String getActivityName(Activity activity) {
        String namePath = activity.getLocalClassName();
        if (namePath.length() > 10) {
            return namePath.substring(10);
        }
        return "";
    }

    public void removeActivity(String className) {
        if (className.length() > 10) {
            className = className.substring(10);
        }
        if (activityHashMap != null && activityHashMap.keySet().contains(className)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                activityHashMap.remove(className, activityHashMap.get(className));
            } else {
                activityHashMap.remove(className);
            }
        }
    }

    public void removeActivity(Activity activity) {
        if (activityHashMap != null && activityHashMap.keySet().contains(getActivityName(activity))) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                activityHashMap.remove(getActivityName(activity), activityHashMap.get(getActivityName(activity)));
            } else {
                activityHashMap.remove(getActivityName(activity));
            }
        }
    }
}
